﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Teleplex.Data.DataAdapters.Interfaces;
using Teleplex.DataModels;

namespace Teleplex.Data.DataAdapters.Adapters
{
    public class TVShowAdapter : ITVShowAdapter
    {

        public TVShow CreateShow(TVShow show)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                show = db.TVShows.Add(show);
                db.SaveChanges();
            }
            return show;
        }

        public List<TVShow> GetAllShows()
        {
            List<TVShow> shows;
            using(ApplicationDbContext db = new ApplicationDbContext())
            {
                shows = db.TVShows.Include("Characters").OrderBy(x => x.Title).ToList();
            }
            return shows;
        }

        public TVShow GetShow(int id)
        {
            TVShow show;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                show = db.TVShows.Include("Characters").Where(x => x.Id == id).FirstOrDefault();
            }
            return show;
        }

        public TVShow UpdateShow(TVShow show)
        {
            TVShow dbShow;
            using(ApplicationDbContext db = new ApplicationDbContext())
            {
                dbShow = db.TVShows.Find(show.Id);
                dbShow.SetProperties(show);
                db.SaveChanges();
            }
            return dbShow;
        }

        public TVShow DeleteShow(int id)
        {
            TVShow show;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                show = db.TVShows.Find(id);
                db.TVShows.Remove(show);
                db.SaveChanges();
            }
            return show;
        }


        public TVCharacter CreateCharacter(TVCharacter character)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                character = db.TVCharacters.Add(character);
                db.SaveChanges();
            }
            return character;
        }

        public List<TVCharacter> GetAllCharacters()
        {
            List<TVCharacter> characters;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                characters = db.TVCharacters.OrderBy(x => x.Name).ToList();
            }
            return characters;
        }

        public TVCharacter GetCharacter(int id)
        {
            TVCharacter character;
            using(ApplicationDbContext db = new ApplicationDbContext())
            {
                character = db.TVCharacters.Find(id);
            }
            return character;
        }

        public TVCharacter UpdateCharacter(TVCharacter character)
        {
            TVCharacter dbCharacter;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                dbCharacter = db.TVCharacters.Find(character.Id);
                dbCharacter.SetProperties(character);
                db.SaveChanges();
            }
            return character;
        }

        public TVCharacter DeleteCharacter(int id)
        {
            TVCharacter character;

            using(ApplicationDbContext db = new ApplicationDbContext())
            {
                character = db.TVCharacters.Find(id);
                db.TVCharacters.Remove(character);
                db.SaveChanges();
            }
            return character;
        }
    }
}
